"""
Flask 应用主入口
"""

from flask import Flask, jsonify, send_from_directory
from .config import config
from .controllers.routes import api_bp


def create_app() -> Flask:
    """创建 Flask 应用实例"""
    app = Flask(__name__)

    # 加载配置
    app.config['SECRET_KEY'] = config.SECRET_KEY
    app.config['DEBUG'] = config.DEBUG

    # 注册蓝图
    app.register_blueprint(api_bp)

    # 提供图表静态文件访问
    @app.route('/static/charts/<path:filename>')
    def serve_chart(filename: str):
        """提供图表文件的静态访问"""
        charts_dir = config.get_charts_dir()
        return send_from_directory(charts_dir, filename)

    @app.route('/', methods=['GET'])
    def hello() -> tuple:
        """
        根路由 - 返回 hello world

        返回固定格式的 JSON 响应
        """
        return jsonify({
            "code": 200,
            "data": {},
            "msg": "hello world"
        }), 200

    return app
